<?php
use common\components\Tree;

$value = $value!='' ? $value : @$setting['defaultvalue'];
$modelid = isset($param['modelid']) ? $param['modelid'] : $_GET['modelid'];
$parentid = isset($param['parentid']) ? intval($param['parentid']) : 0;
$zindex = isset($param['z-index']) ? intval($param['z-index']) : 99;
$width = isset($param['width']) ? intval($param['width']) : 120;
$tree = new Tree();
$tree->icon = array('│','├─ ','└─ ');
$tree->nbsp = '&nbsp;&nbsp;';
$list = Yii::$app->FileCache->get("models_data_".$modelid);

if ($list){
	$models = Yii::$app->FileCache->get("submodule_model_".$modelid);
    $sql="SELECT * FROM {{%{$models['models']['tablename']}}} order by listorder asc, id desc";
	$list = Yii::$app->db->createCommand($sql)->queryAll();	
}
//print_r($list);
$tree->init($list);
$str = "<option value=\$id  \$selected>\$spacer\$name</option>"; //生成的形式
$select = $tree->get_tree($parentid, $str, $value);

?>
<label class="ch-select" z-index="<?php echo $zindex?>" width="<?php echo $width?>">
<select style="visibility:hidden; width:0;" id="<?php echo $id;?>" name="<?php echo $name?>" data-value="<?php echo $value?>" width="<?php echo $width?>">
<option value="0">无</option>
<?php
echo $select;


?>
</select>
</label>
<script>
<?php if(isset($param['linkage']) && $param['linkage']){?>
function <?php echo $id;?>_linkage(){
    $('#<?php echo $id;?>').on('change', function(){
		$.getJSON('<?php echo Yii::$app->controller->getSubmodelActionUrl(['linkage']);?>', {'table': '<?php echo @$param['linkageTable']?>', 'conditionfield': '<?php echo @$param['linkageField']?>', value: $(this).val(), 'field': '<?php echo @$param['linkageValue']?>', 'name': '<?php echo @$param['linkageText']?>'}, function(data){
			var option = [];
			$.each(data, function(i, v){
				option.push('<option value="'+v.<?php echo @$param['linkageValue']?>+'">'+v.<?php echo @$param['linkageText']?>+'</option>');
			});
			data.option = '<option value="">无</option>\n'+data.option;
			$('#<?php echo @$param['linkageId']?>').empty().html(data.option);
			//alert(data)
			beautify_select($('#<?php echo @$param['linkageId']?>').parent());
			
		});
		
	});	
	
}
$(function(){
	<?php echo $id;?>_linkage();
});
<?php
}
?>
</script>